<template>
    <div class="goodsinfo-container">

        <transition 
        @before-enter="beforeEnter"
        @enter="Enter"
        @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>

        <!-- 商品轮播图区 -->
<div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<swiper :lunbotuList="goodslunbotu" :isfull="false"></swiper>
					</div>
				</div>
			</div>
        <!-- 商品购买区 -->
<div class="mui-card">
				<div class="mui-card-header">{{goodsInfo.tital}}</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<div class="price">
                            市场价：<del>￥{{goodsInfo.market_price}}</del>&nbsp;&nbsp;
                            销售价：<span class="now_price">￥{{goodsInfo.sell_price}}<span>
                        </div>
                        <p>购买数量：
                            <numbox @getcount="getSelectCount" :max="goodsInfo.stock_quantity"></numbox>
                        </p>
                        <p>
                            <mt-button type="primary" size="small">立即购买</mt-button>
                            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
                        </p>
					</div>
				</div>
			</div>        

        <!-- 商品参数区 -->
<div class="mui-card">
				<div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p>商品货号：{{goodsInfo.goods_no}}</p>
                        <p>库存数量：{{goodsInfo.stock_quantity}}</p>
                        <p>上架时间：{{goodsInfo.add_time | dateFormat}}</p>
					</div>
				</div>
				<div class="mui-card-footer">
                    <mt-button type="primary" size="large" @click="goDesc(id)">图文介绍</mt-button>
                    <mt-button type="danger" size="large" @click="goComment(id)">商品评论</mt-button>
                </div>
			</div>
    </div>
</template>

<script>
import swiper from '../../subComponents/swiper.vue'
import numbox from '../../subComponents/GoodsInfo_numbox.vue'

export default {
    data(){
        return {
            id: this.$route.params.id,
            goodslunbotu: [],
            goodsInfo:[],
            ballFlag: false,
            selectCount:1,

        }
    },

    created(){
        this.getLunBoTu()
        this.getGoodsInfo()

    },
    methods:{
        getLunBoTu(){
            this.$http.get("api/getthumimage/" + this.id).then( result => {
                if(result.body.status === 0){
                    result.body.message.forEach(item => {
                        item.img = item.src
                    })
                    this.goodslunbotu=result.body.message[0]
                }
            })
        },
        getGoodsInfo(){
            this.$http.get("api/goods/goodslist/" + this.id).then( result => {
                if (result.body.status ===0 ){
                    this.goodsInfo = result.body.message
                }
            })
        },
        goDesc(id){
            this.$router.push({name:"goodsdesc",params:{ id }})

        },
        goComment(id){
            this.$router.push({name:"goodscomment",params:{ id }})

        },
        addToShopCar(){
            this.ballFlag = !this.ballFlag
            var goodsinfo = {
                id:this.id,count:this.selectCount,
                price:this.goodsInfo.sell_price,selected:true}

                this.$store.commit('addToCar',goodsinfo)
        },
        beforeEnter(el){
            el.style.transform = "translate( 0 , 0 )"

        },
        Enter(el,done){
            el.offsetWidth()    
            const ballPosition = this.$refs.ball.getBoundingClientRect()
            const badgePosition = document.ElementById('bdge').getBoundingClientRect()

            const xDist = badgePosition.left - ballPosition.left
            const yDist = badgePosition.top - ballPosition.top

            el.style.transform = "translate($(xDist)px,$(yDist)px)"
            el.style.translate = "all 1s cubic-bezier(.4,-0.3,1,.68)"
            done()

        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag

        },
        getSelectCount(count){
            this.selectCount=count
        }


    },

    components:{
        swiper
    }
}
</script>

<style lang="scss" scoped>
.goodsinfo-container{
    background-color: #eee;
    overflow: hidden;

    .now_price{
        color: red;
        font-size: 14px;
        font-weight: bold;
    }
    .mui-card-foots{
        display: block;
        button{
            margin: 15px 0;
        }
    }
    .ball{
        background-color: red;
        width: 90px;
        height: 90px;
        border-radius: 50%;
        position: absolute;
        z-index: 99px;
        top: 390px;
        left: 146px;

    }
}
</style>